using System.Collections.Generic;
/// <summary>
/// RAM扩展
/// </summary>
public static class HFRAMUnitExtend
{
    /// <summary>
    /// enRAMUnit值名称映射
    /// </summary>
    static readonly Dictionary<ulong, string> smRamValueNameMaping = typeof(enRAMUnit).HFValueToName<ulong>();

    #region 单位转换
    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this byte _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this sbyte _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this short _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this ushort _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this int _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this uint _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this long _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this ulong _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this float _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this decimal _byteSize, enRAMUnit _enRAMUnit)
    {
        return HFToRAM((double)_byteSize, _enRAMUnit);
    }

    /// <summary>
    /// 内存单位转换
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>单位转换后的大小</returns>
    public static double HFToRAM(this double _byteSize, enRAMUnit _enRAMUnit)
    {
        return _byteSize / (ulong)_enRAMUnit;
    }
    #endregion

    #region 单位字符

    #region 无参数
    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this byte _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this sbyte _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this short _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this ushort _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this int _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this uint _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this long _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this ulong _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this float _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this decimal _byteSize)
    {
        return OnHFToRAMChar((double)_byteSize, enRAMUnit.B, true);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this double _byteSize)
    {
        return OnHFToRAMChar(_byteSize, enRAMUnit.B, true);
    }
    #endregion

    #region enRAMUnit 参数
    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this byte _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this sbyte _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this short _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this ushort _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this int _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this uint _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this long _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this ulong _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this float _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this decimal _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar((double)_byteSize, _enRAMUnit, false);
    }

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <returns>转换后的单位表示</returns>
    public static string HFToRAMChar(this double _byteSize, enRAMUnit _enRAMUnit)
    {
        return OnHFToRAMChar(_byteSize, _enRAMUnit, false);
    }
    #endregion

    /// <summary>
    /// 内存单位表示
    /// </summary>
    /// <param name="_byteSize">字节</param>
    /// <param name="_enRAMUnit">单位</param>
    /// <param name="_isAutoUnit">是否是自动单位</param>
    /// <returns>转换后的单位表示</returns>
    static string OnHFToRAMChar(this double _byteSize, enRAMUnit _enRAMUnit, bool _isAutoUnit)
    {
        ulong targetRAMUnit = (ulong)enRAMUnit.B;
        if (_isAutoUnit)
        {
            foreach (KeyValuePair<ulong, string> key in smRamValueNameMaping)
            {
                if (_byteSize >= key.Key)
                {
                    targetRAMUnit = key.Key;
                }
            }
        }
        else
        {
            targetRAMUnit = (ulong)_enRAMUnit;
        }
        return string.Format("{0:F2}{1}", HFToRAM(_byteSize, (enRAMUnit)targetRAMUnit), smRamValueNameMaping[targetRAMUnit]);
    }
    #endregion
}

/// <summary>
/// 内存单位
/// </summary>
public enum enRAMUnit : ulong
{
    /// <summary>
    /// 字节
    /// </summary>
    B = 1,
    /// <summary>
    /// 千字节
    /// </summary>
    KB = B << 10,
    /// <summary>
    /// 兆字节
    /// </summary>
    MB = KB << 10,
    /// <summary>
    /// 十亿字节
    /// </summary>
    GB = MB << 10,
    /// <summary>
    /// 万亿
    /// </summary>
    TB = GB << 10,
}